package game.components;

import java.awt.Color;
import java.io.Serializable;

import remote.AppUtilitiesIfc;
import remote.GameActionIfc;

public class AppUtilities implements Serializable, AppUtilitiesIfc {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int cellSize = 25;
	private int tableSize = 255;
	private int widthOfX_O = 5;
	private int deadlineTime = 10;
	private int totalSec = 0;

	private int playerID;
	private Player winner;
	private boolean gameStatus = false; // true = PLAYING; false = PAUSE
	private boolean multiPlayer = false;
	private boolean sever = false;

	private Color bg_playingArea = new Color(0, 30, 0);
	private Color hlMousePointing = new Color(10, 80, 0); // highlight mouse p
	private Color hlLastMoving = new Color(20, 90, 10); // highlight last moving
	private Color gridColor = new Color(130, 130, 130);
	private Color X_Color = new Color(255, 130, 0);
	private Color O_Color = new Color(0, 255, 255);

	private Player player1;
	private Player player2;
	private transient RuleOfGame ruleOfGame;
	private Player turnOfPlayer; // player1 or player2

	/*
	 * The below gameAction will be initialized after add all needed component
	 * to PlayingScreenUi. The construction will place at the end of function:
	 * PlayingScreenUi.addComponents()
	 */
	private transient GameActionIfc gameAction;

	public AppUtilities() {
		this.player1 = new Player("Player1");
		player1.setID(1);
		this.player2 = new Player("Player2");
		player2.setID(2);

		this.ruleOfGame = new RuleOfGame(this);
		this.setTurnOfPlayer(player1);
	}

	public void resetGame() {
		// set game status
		setGameStatus(true);

		// clear winner
		setWinner(null);

		// increase total section and set label
		setTotalSec(getTotalSec() + 1);

		// clear moving
		player1.getMoveCollection().clear();
		player2.getMoveCollection().clear();
	}

	public int getCellSize() {
		return cellSize;
	}

	public Color getBg_playingArea() {
		return bg_playingArea;
	}

	public Color getGridColor() {
		return gridColor;
	}

	public int getTableSize() {
		return tableSize;
	}

	public Color getX_Color() {
		return X_Color;
	}

	public Color getO_Color() {
		return O_Color;
	}

	public int getWidthOfX_O() {
		return widthOfX_O;
	}

	public Player getPlayer1() {
		return player1;
	}

	public Player getPlayer2() {
		return player2;
	}

	public Player getTurnOfPlayer() {
		return turnOfPlayer;
	}

	public Color getBg_highlightMousePointing() {
		return hlMousePointing;
	}

	public void changeTurn() {
		if (this.turnOfPlayer == this.getPlayer1()) {
			this.setTurnOfPlayer(this.getPlayer2());
		} else {
			this.setTurnOfPlayer(this.getPlayer1());
		}

		// if this is not multiplayer game, then change the player ID
		if (!this.isMultiPlayer()) {
			this.setPlayerID(getTurnOfPlayer().getID());
		}
	}

	public void setTurnOfPlayer(Player player) {
		if (player == player1) {
			player1.setMyTurn(true);
			player2.setMyTurn(false);
		} else {
			player1.setMyTurn(false);
			player2.setMyTurn(true);
		}
		this.turnOfPlayer = player;
	}

	public RuleOfGame getRuleOfGame() {
		return ruleOfGame;
	}

	public Color getHlLastMoving() {
		return hlLastMoving;
	}

	public Player getWinner() {
		return winner;
	}

	public void setWinner(Player winner) {
		this.winner = winner;
	}

	public void saveSession() {
		// TODO Auto-generated method stub

	}

	public boolean isGameStatus() {
		return gameStatus;
	}

	public void setGameStatus(boolean gameStatus) {
		this.gameStatus = gameStatus;
	}

	public int getDeadLineTime() {
		return deadlineTime;
	}

	public void setDeadLineTime(int deadlineTime) {
		this.deadlineTime = deadlineTime;
	}

	public int getTotalSec() {
		return totalSec;
	}

	public void setTotalSec(int totalSec) {
		this.totalSec = totalSec;
	}

	public GameActionIfc getGameAction() {
		return gameAction;
	}

	public void setGameAction(GameActionIfc gameAction) {
		this.gameAction = gameAction;
	}

	public boolean isSever() {
		return sever;
	}

	public void setSever(boolean thisIsSever) {
		this.sever = thisIsSever;
	}

	public boolean isMultiPlayer() {
		return this.multiPlayer;
	}

	public int getPlayerID() {
		return this.playerID;
	}

	public void setPlayerID(int playerID) {
		this.playerID = playerID;
	}

	public void setMultiPlayer(boolean multiPlayer) {
		this.multiPlayer = multiPlayer;
	}
}
